﻿using System;
using System.Configuration;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using VCC.CMS.Components;
using VCC.CMS.Properties;
using VCC.CMS.ViewModels;
using VCC.Lib.Common;
using VCC.Lib.Helpers;
using VCC.Lib.Mail;
using VCC.Lib.Paging;
using VCC.Model;
using MyUtility;
using System.Web.UI.MobileControls;
namespace VCC.CMS.Controllers
{
    [RoleHelper(Roles = RoleSystem.Ad)]
    public class AdController : Controller
    {        
        
        #region HttpGet Index
        public ActionResult Index(int? page)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var param = new AdIndexViewModel();
            try
            {
                if (page == null) page = 1;
                string order = UserState.getUserStateFromRequest("Ad.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("Ad.orderDir", "orderDir", "desc").ToString();
                param.CurentCateId = Convert.ToInt32(UserState.getUserStateFromRequest("Ad.CatID", "CatID", 0));
              //  param.CurentAreaId = Convert.ToInt32(UserState.getUserStateFromRequest("Ad.AreaID", "AreaID", 0));
                param.CurentStateId = Convert.ToInt32(UserState.getUserStateFromRequest("Ad.StateID", "StateID", 0));
                param.CurentSearchFor = Convert.ToInt32(UserState.getUserStateFromRequest("SearchFor", "SearchFor", 0));
                param.CurentSuburbId = Convert.ToInt32(UserState.getUserStateFromRequest("Ad.SuburbID", "SuburbID", 0));
                param.CurentPubStatusId = Convert.ToInt32(UserState.getUserStateFromRequest("Ad.PublishYN", "PublishYN", 0));
                param.PubStatus = Utils.StringToDictionary(Settings.Default.PubStatus, ",");
                param.Keyword = UserState.getUserStateFromRequest("Ad.Keyword", "Keyword", "").ToString();
                param.SearchFor = Utils.StringToDictionary(Settings.Default.AdSearchFor, ",");
                int pageSize = int.Parse(UserState.getUserStateFromRequest("article.pageSize", "PageSize", Settings.Default.PageSize).ToString());
                if (!string.IsNullOrEmpty(param.Keyword))
                {
                    if (param.CurentSearchFor == 5)
                    {
                        param.Ads = eDataContext.Ads
                            .Where(a => (param.CurentCateId > 0 ? (a.Category.ParentCategory.ID == param.CurentCateId || a.CatID == param.CurentCateId) : true))
                            //.Where(a => param.CurentStateId > 0 ? a.StateID == param.CurentStateId : true)
                            //.Where(a => param.CurentRegionId > 0 ? a.RegionID == param.CurentRegionId : true)
                            //.Where(a => param.CurentAreaId > 0 ? a.AreaID == param.CurentAreaId : true)
                            //.Where(a => param.CurentSuburbId > 0 ? a.SuburbID == param.CurentSuburbId : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 1 ? a.PublishYN == true : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 2 ? (a.PublishYN == false || a.PublishYN == null) : true)
                            .Where(a => a.ID.ToString().ToLower().Contains(param.Keyword.ToLower()))
                            .Where(a => a.Status != byte.Parse(AdStatus.Deleted.GetHashCode().ToString()))
                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                            .ToPagedList(page.Value - 1, pageSize);
                    }
                    else if (param.CurentSearchFor == 2)
                    {
                        param.Ads = eDataContext.Ads
                            .Where(a => (param.CurentCateId > 0 ? (a.Category.ParentCategory.ID == param.CurentCateId || a.CatID == param.CurentCateId) : true))
                            //.Where(a => param.CurentStateId > 0 ? a.StateID == param.CurentStateId : true)
                            //.Where(a => param.CurentRegionId > 0 ? a.RegionID == param.CurentRegionId : true)
                            //.Where(a => param.CurentAreaId > 0 ? a.AreaID == param.CurentAreaId : true)
                            //.Where(a => param.CurentSuburbId > 0 ? a.SuburbID == param.CurentSuburbId : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 1 ? a.PublishYN == true : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 2 ? (a.PublishYN == false || a.PublishYN == null) : true)
                            .Where(a => a.Status != byte.Parse(AdStatus.Deleted.GetHashCode().ToString()))
                            .Where(a => a.NameContact.ToLower().Contains(param.Keyword.ToLower()))
                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                            .ToPagedList(page.Value - 1, pageSize);
                    }
                    else if (param.CurentSearchFor == 3)
                    {
                        param.Ads = eDataContext.Ads
                            .Where(a => (param.CurentCateId > 0 ? (a.Category.ParentCategory.ID == param.CurentCateId || a.CatID == param.CurentCateId) : true))
                            //.Where(a => param.CurentStateId > 0 ? a.StateID == param.CurentStateId : true)
                            //.Where(a => param.CurentRegionId > 0 ? a.RegionID == param.CurentRegionId : true)
                            //.Where(a => param.CurentAreaId > 0 ? a.AreaID == param.CurentAreaId : true)
                            //.Where(a => param.CurentSuburbId > 0 ? a.SuburbID == param.CurentSuburbId : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 1 ? a.PublishYN == true : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 2 ? (a.PublishYN == false || a.PublishYN == null) : true)
                            .Where(a => a.Status > 0)
                            .Where(a => a.PhoneContact.ToLower().Contains(param.Keyword.ToLower()))
                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                            .ToPagedList(page.Value - 1, pageSize);
                    }
                    else if (param.CurentSearchFor == 4)
                    {
                        param.Ads = eDataContext.Ads
                            .Where(a => (param.CurentCateId > 0 ? (a.Category.ParentCategory.ID == param.CurentCateId || a.CatID == param.CurentCateId) : true))
                            //.Where(a => param.CurentStateId > 0 ? a.StateID == param.CurentStateId : true)
                            //.Where(a => param.CurentRegionId > 0 ? a.RegionID == param.CurentRegionId : true)
                            //.Where(a => param.CurentAreaId > 0 ? a.AreaID == param.CurentAreaId : true)
                            //.Where(a => param.CurentSuburbId > 0 ? a.SuburbID == param.CurentSuburbId : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 1 ? a.PublishYN == true : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 2 ? (a.PublishYN == false || a.PublishYN == null) : true)
                            .Where(a => a.Status >0)
                            .Where(a => a.EmailContact.ToLower().Contains(param.Keyword.ToLower()))
                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                            .ToPagedList(page.Value - 1, pageSize);
                    }
                    else
                    {
                        param.Ads = eDataContext.Ads
                            .Where(a => (param.CurentCateId > 0 ? (a.Category.ParentCategory.ID == param.CurentCateId || a.CatID == param.CurentCateId) : true))
                            //.Where(a => param.CurentStateId > 0 ? a.StateID == param.CurentStateId : true)
                            //.Where(a => param.CurentRegionId > 0 ? a.RegionID == param.CurentRegionId : true)
                            //.Where(a => param.CurentAreaId > 0 ? a.AreaID == param.CurentAreaId : true)
                            //.Where(a => param.CurentSuburbId > 0 ? a.SuburbID == param.CurentSuburbId : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 1 ? a.PublishYN == true : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 2 ? (a.PublishYN == false || a.PublishYN == null) : true)
                            .Where(a => a.Status >0)
                            .Where(a => a.Title.ToLower().Contains(param.Keyword.ToLower()))
                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                            .ToPagedList(page.Value - 1, pageSize);
                    }
                }
                else
                {
                    param.Ads = eDataContext.Ads
                            .Where(a => (param.CurentCateId > 0 ? (a.Category.ParentCategory.ID == param.CurentCateId || a.CatID == param.CurentCateId) : true))
                            //.Where(a => param.CurentStateId > 0 ? a.StateID == param.CurentStateId : true)
                            //.Where(a => param.CurentRegionId > 0 ? a.RegionID == param.CurentRegionId : true)
                            //.Where(a => param.CurentAreaId > 0 ? a.AreaID == param.CurentAreaId : true)
                            //.Where(a => param.CurentSuburbId > 0 ? a.SuburbID == param.CurentSuburbId : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 1 ? a.PublishYN == true : true)
                            .Where(a => param.CurentPubStatusId != null && param.CurentPubStatusId == 2 ? (a.PublishYN == false || a.PublishYN == null) : true)
                            .Where(a => a.Status >0)
                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                            .ToPagedList(page.Value - 1, pageSize);
                }
                param.CategoryBuiltTree = CategoryManager.BuiltCombobox(param.CurentCateId);
                param.State = eDataContext.States.ToList();
                param.Region = eDataContext.Regions.Where(e => e.StateID == param.CurentStateId).ToList();
                param.Area = eDataContext.Areas.Where(e => e.RegionID == param.CurentRegionId).ToList();
                param.Suburb = eDataContext.Suburbs.Where(e => e.AreaID == param.CurentAreaId).ToList();
                param.SiteRootUrl = Settings.Default.SiteRootUrl;
                ViewData["orderDir"] = orderDir;
                ViewData["order"] = order;
                return View(param);
            }
            catch (Exception exp)
            {
                eDataContext.Dispose();
                ViewData[Messages.MessageHeaderInfo] = MessageType.Error + Messages.Error_Exception + exp.StackTrace.ToString();
                return RedirectToAction("Index", "Home");
            }
        }
        #endregion
        

        #region HttpGet Create
        [RoleHelper(Roles = RoleSystem.AdCreate)]
        public ActionResult Create()
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var param = new AdEditModel();
            try
            {
                param.VIPTypes = Utils.StringToDictionary(Settings.Default.ListTypeAd.ToString(), ",");
                param.CostTypes = eDataContext.CostTypes.ToList();

                param.CategoryBuiltTree = CategoryManager.BuiltCombobox(0);
                HttpContext.Session["AdminUpImageTem"] = string.Format("{0:yyyy/MM/dd}", DateTime.UtcNow).Replace("/", "\\") + "\\" + Utils.RandomString(7) + "\\";
                return View(param);
            }
            catch { eDataContext.Dispose(); ViewData["MessageInfo"] = "Tổng hợp dữ liệu ban đầu gặp sự cố!"; return View(); }
        }
        #endregion

        #region HttpPost Create
        [ValidateInput(false)][HttpPost] [RoleHelper(Roles = RoleSystem.Ad)]
        public ActionResult Create(AdEditModel rdata)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            if (eDataContext.Connection.State == System.Data.ConnectionState.Closed) eDataContext.Connection.Open();
            eDataContext.Transaction = eDataContext.Connection.BeginTransaction();
            try
            {
                DateTime fromdate = Request["StartDate"] != null && Request["StartDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["StartDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now;
                fromdate = fromdate.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute);
                DateTime todate = Request["EndDate"] != null && Request["EndDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["EndDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now.AddDays(30);
                todate = todate.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute);
                rdata.Ad.SuburbID =int.Parse(Request["Ad_SuburbID"]);
                rdata.Ad.StartDate = fromdate;
                rdata.Ad.EndDate = todate;
                rdata.Ad.SortDate = fromdate;
                rdata.Ad.CreatedBy = UserManager.GetUserId();
                rdata.Ad.CreatedDate = DateTime.Now;
                rdata.Ad.ModifyDate = rdata.Ad.CreatedDate;
                rdata.Ad.ViewNumber = 0;
                if (rdata.Ad.VIPTypeID == 5) rdata.Ad.PublishYN = true; else rdata.Ad.PublishYN = false;
                rdata.Ad.Status = byte.Parse(AdStatus.New.GetHashCode().ToString());
                eDataContext.Ads.InsertOnSubmit(rdata.Ad);
                eDataContext.SubmitChanges();
                // Upload image descriptions - gallery
                //Check image exist
                string[] listImageT = Utils.GetImageInFolder(Settings.Default.SiteRootPath, Settings.Default.AdImageUploadTemp + Session["AdminUpImageTem"].ToString());
                if (listImageT.Count() > 0)
                {
                    string MiddlePath = string.Format("{0:yyyy/MM/dd}", rdata.Ad.CreatedDate).Replace("/", "\\");
                    MiddlePath = MiddlePath + "\\" + rdata.Ad.ID + "\\";
                    int i = 0;
                    foreach (string fFullPath in listImageT)
                    {
                        var r = new Gallery();
                        r.AdID = rdata.Ad.ID;
                        r.CreatedDate = DateTime.Now;
                        eDataContext.Galleries.InsertOnSubmit(r);
                        eDataContext.SubmitChanges();
                        string setfName = TextHelper.ToUrlFriendly(rdata.Ad.Title);
                        string xtendname = fFullPath.Split('.').LastOrDefault();
                        string fileName = setfName + "_" + rdata.Ad.ID.ToString() + "_" + r.ID.ToString() + "." + xtendname;
                        r.Path = Utils.ReduceImageSizeThenDelete(fileName, Settings.Default.AdImageDescriptionPath + MiddlePath, Settings.Default.SiteRootPath, Convert.ToInt32(Settings.Default.RedureSizePercent), fFullPath);
                        // Insert Ad Avata first image
                        if (i == 0)
                        {
                            int adWidth = Convert.ToInt32(Settings.Default.GenThumbAdWidth.ToString());
                            int adHeight = Convert.ToInt32(Settings.Default.GenThumbAdHeight.ToString());
                            var img = Utils.CreateThumbnail(Settings.Default.SiteRootPath + r.Path.Replace('/', '\\'), adWidth, adHeight);
                            rdata.Ad.AvataImage = Utils.SaveThumbnail(img, adWidth, adHeight, fileName, Settings.Default.AdImageAvatarPath + MiddlePath, Settings.Default.SiteRootPath);
                        }
                        listImageT[i] = string.Empty;
                        i++;
                    }
                }
                eDataContext.SubmitChanges();
                eDataContext.Transaction.Commit();
            }
            catch (Exception exp)
            {
                eDataContext.Transaction.Rollback();
                eDataContext.Dispose();
                ViewData["MessageInfo"] = exp.Message;
                return View(rdata);
            }
            ViewData[Messages.MessageTopInfo] = MessageType.Message + Messages.Add_CreateAd_Succ;
            return RedirectToAction("Index");
        }
        #endregion
        
        [HttpGet]
        public ActionResult Edit(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            AdEditModel model = new AdEditModel();
            try
            {
                model.Ad = eDataContext.Ads.FirstOrDefault(e => e.ID == id);
                model.CategoryBuiltTree = CategoryManager.BuiltCombobox(0);
                model.VIPTypes = Utils.StringToDictionary(Settings.Default.ListTypeAd.ToString(), ",");
                model.AdStatusList = Utils.StringToDictionary(Settings.Default.AdStatus.ToString(), ",");
                model.CostTypes = eDataContext.CostTypes.ToList();
                model.Galleries = eDataContext.Galleries.Where(e => e.AdID == id).ToList();
                model.SiteRootUrl = Settings.Default.SiteRootUrl;
                return View(model);
            }
            catch (Exception ex)
            {
                ViewData[Messages.MessageHeaderInfo] = Messages.Error_Exception + ex.StackTrace;
                return View(model);
            }
        }
        
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id , AdEditModel fromdata)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                fromdata.Ad = eDataContext.Ads.FirstOrDefault(e => e.ID == id);
                DateTime temp = fromdata.Ad.StartDate.Value;
                UpdateModel(fromdata);
                fromdata.Ad.ModifyBy = UserManager.GetUserId();
                fromdata.Ad.ModifyDate = DateTime.Now;
                fromdata.Ad.StartDate = Request["StartDate"] != null && Request["StartDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["StartDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now;
                fromdata.Ad.StartDate = fromdata.Ad.StartDate.Value.AddHours(temp.Hour).AddMinutes(temp.Minute);
                fromdata.Ad.EndDate = Request["EndDate"] != null && Request["EndDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["EndDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now;
                fromdata.Ad.EndDate = fromdata.Ad.EndDate.Value.AddHours(temp.Hour).AddMinutes(temp.Minute);

                // Upload image descriptions - gallery
                //Check image exist
                if (Session["AdminUpImageTem"] != null)
                {
                    string[] listImageT = Utils.GetImageInFolder(Settings.Default.SiteRootPath, Settings.Default.AdImageUploadTemp + Session["AdminUpImageTem"].ToString());
                    if (listImageT.Count() > 0)
                    {
                        string MiddlePath = string.Format("{0:yyyy/MM/dd}", fromdata.Ad.CreatedDate).Replace("/", "\\");
                        MiddlePath = MiddlePath + "\\" + fromdata.Ad.ID + "\\";
                        int i = 0;
                        foreach (string fFullPath in listImageT)
                        {
                            var r = new Gallery();
                            r.AdID = fromdata.Ad.ID;
                            r.CreatedDate = DateTime.Now;
                            eDataContext.Galleries.InsertOnSubmit(r);
                            eDataContext.SubmitChanges();
                            string setfName = TextHelper.ToUrlFriendly(fromdata.Ad.Title);
                            string xtendname = fFullPath.Split('.').LastOrDefault();
                            string fileName = setfName + "_" + fromdata.Ad.ID.ToString() + "_" + r.ID.ToString() + "." + xtendname;
                            r.Path = Utils.ReduceImageSizeThenDelete(fileName, Settings.Default.AdImageDescriptionPath + MiddlePath, Settings.Default.SiteRootPath, Convert.ToInt32(Settings.Default.RedureSizePercent), fFullPath);
                            // Insert Ad Avata first image
                            if (i == 0 && string.IsNullOrEmpty(fromdata.Ad.AvataImage))
                            {
                                int adWidth = Convert.ToInt32(Settings.Default.GenThumbAdWidth.ToString());
                                int adHeight = Convert.ToInt32(Settings.Default.GenThumbAdHeight.ToString());
                                var img = Utils.CreateThumbnail(Settings.Default.SiteRootPath + r.Path.Replace('/', '\\'), adWidth, adHeight);
                                fromdata.Ad.AvataImage = Utils.SaveThumbnail(img, adWidth, adHeight, fileName, Settings.Default.AdImageAvatarPath + MiddlePath, Settings.Default.SiteRootPath);
                            }
                            listImageT[i] = string.Empty;
                            i++;
                        }
                    }
                }
                eDataContext.SubmitChanges();
                ViewData[Messages.MessageTopInfo] = MessageType.Message + Messages.Edit_EditAd_Succ;
                return RedirectToAction("Index");
            }
            catch (Exception  exp)
            {
                ViewData[Messages.MessageTopInfo] = MessageType.Error + Messages.Error_Exception + exp.StackTrace.ToString();
                fromdata.CategoryBuiltTree = CategoryManager.BuiltCombobox(0);
                fromdata.CostTypes = eDataContext.CostTypes.ToList();
                fromdata.VIPTypes = Utils.StringToDictionary(Settings.Default.ListTypeAd.ToString(), ",");
                fromdata.CostTypes = eDataContext.CostTypes.ToList();
                fromdata.Galleries = eDataContext.Galleries.Where(e => e.AdID == id).ToList();
                return View(fromdata);    
            }
        }
        /*
        public ActionResult DetailsCms(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var param = new AdEditCMSViewModel();
            try
            {
                param.Ad = eDataContext.Ads.FirstOrDefault(e => e.ID == id);
                param.Galleries = eDataContext.Galleries.Where(e => e.AdID == id).ToList();
                return View(param);
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }

        public ActionResult UpdateModifileDate()
        {
            int page = 1;
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            
            
                for (int x = 1; x < 4900; x++)
                {
                    IPagedList<Ad> Ads = eDataContext.Ads.Where(e => e.ModifyDate == null).ToPagedList(page - 1, 20);
                    if (Ads.Count() > 0)
                    {
                        foreach (var item in Ads)
                        {
                            item.ModifyDate = item.CreatedDate;
                        }
                        eDataContext.SubmitChanges();
                    }
                }
                return RedirectToAction("IndexCMS", "Home");
            
        }
        #endregion

        public JsonResult DeleteImg(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                var item = eDataContext.Galleries.Where(e => e.ID == id).FirstOrDefault();
                //var fullpath = System.AppDomain.CurrentDomain.BaseDirectory + item.Path.Replace('/', '\\');
                var fullpath = Settings.Default.AdminRootPath + item.Path.Replace('/', '\\');
                Uploads.DeleteFile(fullpath);
                eDataContext.Galleries.DeleteOnSubmit(item);
                eDataContext.SubmitChanges();
                return Json(true);
            }
            catch (Exception exp)
            {
                Response.Write(exp.Message);
                return Json(false);
            }
        }

        public JsonResult RefreshAd(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                var item = eDataContext.Ads.Where(e => e.ID == id).FirstOrDefault();
                if (item != null)
                {
                    item.ModifyDate = DateTime.Now;
                    item.RefreshNumber = string.IsNullOrEmpty(item.RefreshNumber.ToString()) ? 1 : item.RefreshNumber + 1;
                }
                eDataContext.SubmitChanges();
                return Json(true);
            }
            catch (Exception exp)
            {
                Response.Write(exp.Message);
                return Json(false);
            }
        }

        public JsonResult DeleteListAd(string listId)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            //var listId = Request["ListID"];
            if (!string.IsNullOrEmpty(listId))
            {
                var lstId = listId.Split(',');
                // Delete Ad
                foreach (var itemId in lstId)
                {
                    if (!string.IsNullOrEmpty(itemId))
                    {
                        var id = Convert.ToInt32(itemId);
                        var curentAd = eDataContext.Ads.FirstOrDefault(e => e.ID == id);
                        if (curentAd != null)
                        {
                            // Delete avatar ad imge
                            if (!string.IsNullOrEmpty(curentAd.Image))
                            {
                                var path = Settings.Default.AdminRootPath + curentAd.Image.Replace('/', '\\');
                                Uploads.DeleteFile(path);
                            }

                            // Delete description imge
                            var lst = eDataContext.Galleries.Where(e => e.AdID == id).ToList();
                            if (lst != null && lst.Count() > 0)
                            {
                                foreach (var item in lst)
                                {
                                    var itempath = Settings.Default.AdminRootPath + item.Path.Replace('/', '\\');
                                    Uploads.DeleteFile(itempath);
                                }
                                eDataContext.Galleries.DeleteAllOnSubmit(lst);
                            }
                            // Delete AdTagMap
                            var tagmaps = eDataContext.AdTagMaps.Where(e => e.AdID == id).ToList();
                            if (tagmaps != null && tagmaps.Count() > 0) { eDataContext.AdTagMaps.DeleteAllOnSubmit(tagmaps); }
                            // Delete all Comment
                            var comments = eDataContext.Comments.Where(e => e.AdID == id).ToList();
                            if (comments != null && comments.Count() > 0) { eDataContext.Comments.DeleteAllOnSubmit(comments); }
                            //Delete adAdtributema
                            var adadtributemap = eDataContext.AdAttributeMaps.Where(e => e.AdID == id).ToList();
                            if (adadtributemap != null && adadtributemap.Count() > 0)
                            {
                                eDataContext.AdAttributeMaps.DeleteAllOnSubmit(adadtributemap);
                            }
                            // Delete FeedBack
                            var feedback = eDataContext.Feedbacks.Where(e => e.AdID == id).ToList();
                            if (feedback != null && feedback.Count() > 0) eDataContext.Feedbacks.DeleteAllOnSubmit(feedback);
                            eDataContext.SubmitChanges();
                            eDataContext.Ads.DeleteOnSubmit(curentAd);
                            eDataContext.SubmitChanges();
                        }
                    }
                }
                return Json(true);
            }
            return Json(false);
        }

        */
    }
}